package com.syni.mdd.sjb.tool.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.common.mapper.DxshAppUserInfoMapper;
import com.syni.mdd.sjb.common.component.utils.common.enums.CommonResultEnum;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.tool.entity.vo.requestVo.UserBindWxVo;
import com.syni.mdd.sjb.tool.entity.vo.responseVo.UserBindWxRpVo;
import com.syni.mdd.sjb.tool.service.DxshAppUserInfoService;
import com.syni.mdd.sjb.tool.utils.WeChatUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电享生活商户APP 个人资料信息 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-04-27
 */
@Service
@Log4j2
public class DxshAppUserInfoServiceImpl extends ServiceImpl<DxshAppUserInfoMapper, DxshAppUserInfo> implements DxshAppUserInfoService {

    @Autowired
    DxshAppUserInfoMapper dxshAppUserInfoMapper;

    @Override
    public boolean bindWx(Integer userId, String unionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.USER_ID,userId);
        List<DxshAppUserInfo> dxshAppUserInfos = dxshAppUserInfoMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            dxshAppUserInfos.get(0).setUnionId(unionId);
            dxshAppUserInfoMapper.updateById(dxshAppUserInfos.get(0));
        }else {
            DxshAppUserInfo dxshAppUserInfo = new DxshAppUserInfo();
            dxshAppUserInfo.setUnionId(unionId);
            dxshAppUserInfo.setUserId(userId);
            dxshAppUserInfo.setNewTime(LocalDateTime.now());
            dxshAppUserInfo.setUpdateTime(LocalDateTime.now());
            dxshAppUserInfoMapper.insert(dxshAppUserInfo);
        }
        return true;
    }

    @Override
    public DxshAppUserInfo getByUniondId(String unionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.UNION_ID,unionId);
        List<DxshAppUserInfo> dxshAppUserInfos = dxshAppUserInfoMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            return dxshAppUserInfos.get(0);
        }
        return null;
    }

    @Override
    public Map<String,Object> appBindWx(UserBindWxVo userBindWxVo) {
        //判断这个unionid是否已经绑定其他手机号了
        DxshAppUserInfo dxshAppUserInfo = this.getByUniondId(userBindWxVo.getUnionId());
        if(dxshAppUserInfo != null && !dxshAppUserInfo.getUserId().equals(userBindWxVo.getUserId())){
            return ResultMapHelper.result(CommonResultEnum.WX_BIND_EXIST);
        }
        DxshAppUserInfo userInfo = this.appBindWxOperation(userBindWxVo);
        if(userInfo != null){
            UserBindWxRpVo userBindWxRpVo = new UserBindWxRpVo();
            userBindWxRpVo.setUnionid(userInfo.getUnionId());
            userBindWxRpVo.setWechatName(userInfo.getWechatName());
            userBindWxRpVo.setWechatHeadUrl(userInfo.getWechatHeadUrl());
            return ResultMapHelper.success(userBindWxRpVo);
        }
        return ResultMapHelper.result(CommonResultEnum.WX_BIND_FAIL);
    }

    public DxshAppUserInfo appBindWxOperation(UserBindWxVo userBindWxVo){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DxshAppUserInfo.USER_ID,userBindWxVo.getUserId());
        List<DxshAppUserInfo> dxshAppUserInfos = dxshAppUserInfoMapper.selectList(queryWrapper);
        DxshAppUserInfo dxshAppUserInfo;
        if(CollectionUtils.isNotEmpty(dxshAppUserInfos)){
            dxshAppUserInfo = dxshAppUserInfos.get(0);
        }else {
            dxshAppUserInfo = new DxshAppUserInfo();
            dxshAppUserInfo.setUserId(userBindWxVo.getUserId());
            dxshAppUserInfo.setNewTime(LocalDateTime.now());
            dxshAppUserInfo.setUpdateTime(LocalDateTime.now());
            dxshAppUserInfoMapper.insert(dxshAppUserInfo);
        }
        dxshAppUserInfo.setUnionId(userBindWxVo.getUnionId());
        dxshAppUserInfo.setOpenId(userBindWxVo.getOpenId());
        JSONObject jsonObject = WeChatUtils.findWeChatUserInfo(dxshAppUserInfo.getOpenId(),userBindWxVo.getAccessToken());
        log.info("appBindWxOperation:{}",jsonObject.toJSONString());
        if(jsonObject.containsKey("nickname")){
            dxshAppUserInfo.setWechatName(jsonObject.getString("nickname"));
        }
        if(jsonObject.containsKey("headimgurl")){
            dxshAppUserInfo.setWechatHeadUrl(jsonObject.getString("headimgurl"));
        }
        dxshAppUserInfoMapper.updateById(dxshAppUserInfo);
        return dxshAppUserInfo;
    }
}
